package com.zby.lease.web.admin.service.imp;


import com.wf.captcha.SpecCaptcha;
import com.zby.lease.common.constant.RedisConstant;
import com.zby.lease.common.exception.LeaseException;
import com.zby.lease.common.result.ResultCodeEnum;
import com.zby.lease.common.security.SecurityUtil;
import com.zby.lease.common.utils.JwtUtil;
import com.zby.lease.model.enums.BaseStatus;
import com.zby.lease.model.pojo.SystemUser;
import com.zby.lease.web.admin.mapper.SystemUserMapper;
import com.zby.lease.web.admin.service.LoginService;
import com.zby.lease.web.admin.vo.login.CaptchaVo;
import com.zby.lease.web.admin.vo.login.LoginVo;
import com.zby.lease.web.admin.vo.system.user.SystemUserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SecurityUtil securityUtil;
    @Override
    public String login(LoginVo loginVo) {
    // 检验是否输入验证码
        if (StringUtils.isEmpty(loginVo.getCaptchaKey()) || StringUtils.isEmpty(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        // String captchaCode = loginVo.getCaptchaCode();
        // 从Redis中获取验证码
        String code = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        // 检验验证码是否正确
        if (!code.equalsIgnoreCase(loginVo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        if (StringUtils.isEmpty(code)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
       SystemUser systemUser = systemUserMapper.selectUserName(loginVo.getUsername());
        //  检验用户名是否存在
        if (systemUser == null || systemUser.getUsername() == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //4.校验用户是否被禁
        if (systemUser.getStatus() == BaseStatus.DISABLE) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //  检验是否输入密码
        if (StringUtils.isEmpty(loginVo.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //  检验密码是否正确
        if (!securityUtil.checkPassword(loginVo.getPassword(), systemUser.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        return JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());
    }

    @Override
    public SystemUserInfoVo getUserInfo(long userId) {
        SystemUser systemUser = systemUserMapper.selectById(userId);
        if (systemUser.getId() != null){
            SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
            systemUserInfoVo.setName(systemUser.getUsername());
            systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());
            return systemUserInfoVo;
        }
        return null;
    }


    @Override
    public CaptchaVo getCaptcha() {
        // 创建一个SpecCaptcha对象，参数分别为图片宽度、高度和验证码字符数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);

        // 设置验证码字符类型为纯数字
        specCaptcha.setCharType(SpecCaptcha.TYPE_ONLY_NUMBER);

        // 生成验证码字符串，并转换为小写
        String code = specCaptcha.text().toLowerCase();

        // 生成一个唯一的键，用于存储验证码信息
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();

        // 将验证码图片转换为Base64编码的字符串
        String image = specCaptcha.toBase64();

        // 使用Redis存储验证码信息，设置键值对和过期时间
        redisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        // 返回包含验证码图片和唯一键的CaptchaVo对象
        return new CaptchaVo(image, key);
    }
}
